#include "saltnoise.h"
#include "ui_saltnoise.h"

saltnoise::saltnoise(QWidget *parent) : QMainWindow(parent),
                                        ui(new Ui::saltnoise)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_QuitOnClose, false);
    init_saltnoise();
}

saltnoise::~saltnoise()
{
    delete ui;
}

void saltnoise::init_saltnoise()
{

    ui->checkbtn_t1->setChecked(true);
    connect(ui->slider_snr, SIGNAL(valueChanged(int)), ui->spinbox_snr, SLOT(setValue(int)));
    connect(ui->spinbox_snr, SIGNAL(valueChanged(int)), ui->slider_snr, SLOT(setValue(int)));
    connect(ui->slider_snr, SIGNAL(valueChanged(int)), this, SLOT(img_preview()));
    connect(ui->check_preview, SIGNAL(clicked()), this, SLOT(img_preview()));
    connect(ui->checkbtn_t1, SIGNAL(clicked()), this, SLOT(img_preview()));
    connect(ui->checkbtn_t2, SIGNAL(clicked()), this, SLOT(img_preview()));
    connect(ui->btn_y, SIGNAL(clicked()), this, SLOT(btn_y_clicked()));
    connect(ui->btn_n, SIGNAL(clicked()), this, SLOT(btn_n_clicked()));
}
void saltnoise::img_preview()
{
    if (ui->check_preview->isChecked())
    {
        int t;
        if (ui->checkbtn_t1->isChecked())
        {
            t = 0;
        }
        else
        {
            t = 255;
        }
        double snr;
        snr = 1 - ui->slider_snr->value() / 100.0;
        emit sendData(snr, t, 2);
    }
    else
    {
        emit sendData(0, 0, 0);
    }
}
void saltnoise::btn_y_clicked()
{
    int t;
    if (ui->checkbtn_t1->isChecked())
    {
        t = 0;
    }
    else
    {
        t = 255;
    }
    double snr;
    snr = 1 - ui->slider_snr->value() / 100.0;
    emit sendData(snr, t, 1);
    this->close();
}
void saltnoise::btn_n_clicked()
{
    emit sendData(0, 0, 0);
    this->close();
}
void saltnoise::closeEvent(QCloseEvent *event)
{
    emit sendData(0, 0, 0);
    event->accept();
}